xquery version "3.0";

module namespace back-form = "forms/back-form";


declare function back-form:html(){
    util:declare-option("exist:serialize", "method=html5 media-type=text/html indent=no"),
    <html>
        <head>
            <link rel="stylesheet" type="text/css" href="bootstrap.min.css"/>
            <link rel="stylesheet" type="text/css" href="style.css"/>
            <script src="jquery.min.js" type="text/javascript"></script>
            <script src="script.js" type="text/javascript"></script>
        </head>
        <body>
            {
                transform:transform(
                    doc("/db/tk_data/messages/messages.xml")/messages/record, 
                    doc("/db/tk/exist-db/modules/message-template.xsl"), 
                    ()
                )
            }
            <h1>Оставить сообщение</h1>
            <form id="back-post-form" method="POST" enctype="multipart/form-data">
                <label for="e-mail">e-mail</label><input type="email" name="e-mail"/>
                <label for="name">Имя</label><input type="text" name="name"/>
                <label for="image">Фото</label><input type="file" name="image"/>
                <label for="message">Текст сообщения</label><textarea name="message" rows="10" cols="50"/>
                <input type="submit" value="Отправить"/>
            </form>
        </body>
    </html>
};

declare function back-form:store(){
    if ("delete" = request:get-parameter-names()) then (
        let $record := doc("/db/tk_data/messages/messages.xml")/messages/record[@m_id = request:get-parameter("delete", "")]
        return (
            xmldb:login("/db/", "vitalik", "12"),
            try{xmldb:remove("/db/tk_data/images/user-images/", $record/image/string())} catch *{()},
            update delete $record,
            response:redirect-to(xs:anyURI(concat(request:get-header("Origin"), "/feed")))
        )
    )
    else if (request:get-method() = "POST") then (
        let $agent-info := <agent>
            {
                for $name in request:get-header-names()
                return <header name="{$name}">{request:get-header($name)}</header>
            }
        </agent>
        let $login := xmldb:login("/db/", "vitalik", "12")
        
        let $record-id := util:uuid()
        let $name := concat($record-id, ".jpeg")
        
        (:let $img-name := try {:)
            let $img-name := if (request:get-uploaded-file-size("image") gt 0) then (
                let $s := xmldb:store(
                    "/db/tk_data/images/user-images/",
                    $name,
                    request:get-uploaded-file-data("image"),
                    "image/jpeg"
                ) return $name
            ) else ""
            (:return $img-name
        } catch * {
            response:stream(<error/>, "method=html5 media-type=text/html indent=no")
        }:)
            
        let $record := <record e-mail="{request:get-parameter("e-mail", "")}" date="{util:system-dateTime()}" m_id="{$record-id}">
            {$agent-info}
            <content>{request:get-parameter("message", "")}</content>
            <name>{request:get-parameter("name", "")}</name>
            <image>{$img-name}</image>
        </record>
        let $store := update insert $record into doc("/db/tk_data/messages/messages.xml")/messages
        
        return (:response:stream-binary(request:get-uploaded-file-data("image"), "image", "name.jpg"):) 
        back-form:html()
    )
    else back-form:html()
};